#include "modbus_rtu_master.h"
#include "../function_interface.h"

#define SR_BUFFER_LENGTH 1000
static uint16_t sendRecvBuffer[SR_BUFFER_LENGTH];

#if (FAN_TYPE == NEW_FAN)
void ModbusRequest_Fan1Speed(modbus_t *ctx)
{
    Modbus_info_t fan_info;
    int ret = 0;
    uint16_t speed = 0;
    uint16_t test;
    const ConfigData *config_data = get_config_data_instance();

    fan_info.sid = (config_data->fan1).slave_id;
    fan_info.addr = 0xD001;
    fan_info.fc = 0x06;
    // GlobalVariable_GetInt32(GV_INT32_FAN1_TEST, &val);
    FuncInf_GetTestSwitch(FUNCINF_TEST_SWITCH_TYPE_FAN1, &test);
    if(test != 0)
    {
        fan_info.val = 64000;
    }else
    {
        speed = FanControl_SpeedGet();
        fan_info.val = speed * 640;
    }
    printf("[%s][%d] fan_info.val[%d]\n", __func__, __LINE__, fan_info.val);
    fan_info.num = 1;
    ret = modbus_send_message_handler(ctx, &fan_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_FAN1);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_Fan2Speed(modbus_t *ctx)
{
    Modbus_info_t fan_info;
    int ret = 0;
    uint16_t speed = 0;
    uint16_t test;
    const ConfigData *config_data = get_config_data_instance();

    fan_info.sid = (config_data->fan2).slave_id;
    fan_info.addr = 0xD001;
    fan_info.fc = 0x06;
    // GlobalVariable_GetInt32(GV_INT32_FAN2_TEST, &val);
    FuncInf_GetTestSwitch(FUNCINF_TEST_SWITCH_TYPE_FAN2, &test);
    if(test != 0)
    {
        fan_info.val = 64000;
    }else
    {
        speed = FanControl_SpeedGet();
        fan_info.val = speed * 640;
    }
    printf("[%s][%d] fan_info.val[%d]\n", __func__, __LINE__, fan_info.val);
    fan_info.num = 1;

    ret = modbus_send_message_handler(ctx, &fan_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_FAN2);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_ISOBoardFreqSet(modbus_t *ctx)
{
    Modbus_info_t iso_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();
    
    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 19;
    iso_info.fc = 0x06;
    iso_info.val = 60;
    iso_info.num = 1;

    ret = modbus_send_message_handler(ctx, &iso_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_ISOBoardCsSet(modbus_t *ctx)
{
    Modbus_info_t iso_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();
    
    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 20;
    iso_info.fc = 0x06;
    iso_info.val = 2000;
    iso_info.num = 1;

    ret = modbus_send_message_handler(ctx, &iso_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_ISOBoardCmdSet(modbus_t *ctx)
{
    Modbus_info_t iso_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();

    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 21;
    iso_info.fc = 0x06;
    iso_info.val = 1;
    iso_info.num = 1;

    ret = modbus_send_message_handler(ctx, &iso_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ParseModbusReply_ISOBoardAllInfor(uint16_t *pBuf)
{
    isoBoardInfor_t isoBoardInf;
    uint16_t ofs = 0, tempNo;

    /*
     * [1] - 电抗器
     * [2] - 母排电容
     * [4] - 控制变
     * [5] - 交流柜 
     * [6] - 滤波电容2
     * [7] - 滤波电容1
     */
    for(tempNo = 0; tempNo < 8; tempNo++) 
    {
        isoBoardInf.temp[tempNo] = (int16_t)(pBuf[ofs]);
        ofs += 1;
        isoBoardInf.temp[tempNo] -= 2730;
        isoBoardInf.temp[tempNo] = (isoBoardInf.temp[tempNo] < -400 ? 0 : isoBoardInf.temp[tempNo]);
    }

    isoBoardInf.volGridAB = pBuf[ofs];
    ofs += 1;
    isoBoardInf.volGridBC = pBuf[ofs];
    ofs += 1;
    isoBoardInf.volGridCA = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoResPos = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoResNeg = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curGridA = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curGridB = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curGridC = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curLeak = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoVolPos = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoVolNeg = pBuf[ofs];

    SetIsoBoardInfor(&isoBoardInf);

    for(tempNo = 0; tempNo < 8; tempNo++) 
    {
		printf("\ttemp%d[%d]\n", tempNo, isoBoardInf.temp[tempNo]);
	}
	printf("\tvolGridAB[%d]\n", isoBoardInf.volGridAB);
	printf("\tvolGridBC[%d]\n", isoBoardInf.volGridBC);
	printf("\tvolGridCA[%d]\n", isoBoardInf.volGridCA);
	printf("\tisoResPos[%d]\n", isoBoardInf.isoResPos);
	printf("\tisoResNeg[%d]\n", isoBoardInf.isoResNeg);
	printf("\tcurGridA[%d]\n", isoBoardInf.curGridA);
	printf("\tcurGridB[%d]\n", isoBoardInf.curGridB);
	printf("\tcurGridC[%d]\n", isoBoardInf.curGridC);
	printf("\tcurLeak[%d]\n",  isoBoardInf.curLeak);
	printf("\tisoVolPos[%d]\n", isoBoardInf.isoVolPos);
	printf("\tisoVolNeg[%d]\n", isoBoardInf.isoVolNeg);
    return;
}
void ModbusRequest_ISOBoardAllInfor(modbus_t *ctx, uint16_t *pBuf)
{
    Modbus_info_t iso_info;
    int ret = 0; 
    const ConfigData *config_data = get_config_data_instance();
    
    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 0;
    iso_info.fc = 0x03;
    iso_info.num = 19;
    iso_info.val = 0;

    ret = modbus_send_message_handler(ctx, &iso_info, pBuf);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
        ParseModbusReply_ISOBoardAllInfor(pBuf);
    }
}
#elif(FAN_TYPE == OLD_FAN)
void ModbusRequest_Fan1TurnOn(modbus_t *ctx)
{
    if(NULL == ctx)
    {
        return;
    }

    Modbus_info_t fan_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();
    
    fan_info.sid = (config_data->fan1).slave_id;
    fan_info.addr = 1;
    fan_info.fc = 0x06;
    fan_info.val = strcmp(((config_data->fan1).enable), "true") == 0 ? 1 : 0;
    fan_info.num = 1; 
    
    ret = modbus_send_message_handler(ctx, &fan_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_FAN1);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }   
}
void ModbusRequest_Fan1Speed(modbus_t *ctx)
{
    Modbus_info_t fan_info;
    int ret = 0;
    uint16_t speed = 0;
    uint16_t test;
    const ConfigData *config_data = get_config_data_instance();

    fan_info.sid = (config_data->fan1).slave_id;
    fan_info.addr = 2;
    fan_info.fc = 0x06;
    // GlobalVariable_GetInt32(GV_INT32_FAN1_TEST, &val);
    FuncInf_GetTestSwitch(FUNCINF_TEST_SWITCH_TYPE_FAN1, &test);
    if(test != 0)
    {
        fan_info.val = 100;
    }else
    {
        speed = FanControl_SpeedGet();
        fan_info.val = speed;
    }
    printf("[%s][%d] fan_info.val[%d]\n", __func__, __LINE__, fan_info.val);
    fan_info.num = 1;
    ret = modbus_send_message_handler(ctx, &fan_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_FAN1);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_Fan2TurnOn(modbus_t *ctx)
{
    if(NULL == ctx)
    {
        return;
    }

    Modbus_info_t fan_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();

    fan_info.sid = (config_data->fan2).slave_id;
    fan_info.addr = 1;
    fan_info.fc = 0x06;
    fan_info.val = strcmp(((config_data->fan2).enable), "true") == 0 ? 1 : 0;
    fan_info.num = 1;

    ret = modbus_send_message_handler(ctx, &fan_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_FAN2);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_Fan2Speed(modbus_t *ctx)
{
    Modbus_info_t fan_info;
    int ret = 0;
    uint16_t speed;
    uint16_t test;
    const ConfigData *config_data = get_config_data_instance();

    fan_info.sid = (config_data->fan2).slave_id;
    fan_info.addr = 2;
    fan_info.fc = 0x06;
    // GlobalVariable_GetInt32(GV_INT32_FAN2_TEST, &val);
    FuncInf_GetTestSwitch(FUNCINF_TEST_SWITCH_TYPE_FAN2, &test);
    if(test != 0)
    {
        fan_info.val = 100;
    }else
    {
        speed = FanControl_SpeedGet();
        fan_info.val = speed;
    }
    printf("[%s][%d] fan_info.val[%d]\n", __func__, __LINE__, fan_info.val);
    fan_info.num = 1;

    ret = modbus_send_message_handler(ctx, &fan_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_FAN2);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_ISOBoardFreqSet(modbus_t *ctx)
{
    Modbus_info_t iso_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();
    
    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 19;
    iso_info.fc = 0x06;
    iso_info.val = 60;
    iso_info.num = 1;

    ret = modbus_send_message_handler(ctx, &iso_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_ISOBoardCsSet(modbus_t *ctx)
{
    Modbus_info_t iso_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();
    
    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 20;
    iso_info.fc = 0x06;
    iso_info.val = 2000;
    iso_info.num = 1;

    ret = modbus_send_message_handler(ctx, &iso_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}
void ModbusRequest_ISOBoardCmdSet(modbus_t *ctx)
{
    Modbus_info_t iso_info;
    int ret = 0;
    const ConfigData *config_data = get_config_data_instance();

    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 21;
    iso_info.fc = 0x06;
    iso_info.val = 1;
    iso_info.num = 1;

    ret = modbus_send_message_handler(ctx, &iso_info, NULL);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
    }
}

void ParseModbusReply_ISOBoardAllInfor(uint16_t *pBuf)
{
    isoBoardInfor_t isoBoardInf;
    uint16_t ofs = 0, tempNo;

    /*
     * [1] - 电抗器
     * [2] - 母排电容
     * [4] - 控制变
     * [5] - 交流柜 
     * [6] - 滤波电容2
     * [7] - 滤波电容1
     */
    for(tempNo = 0; tempNo < 8; tempNo++) 
    {
        isoBoardInf.temp[tempNo] = (int16_t)(pBuf[ofs]);
        ofs += 1;
        isoBoardInf.temp[tempNo] -= 2730;
        isoBoardInf.temp[tempNo] = (isoBoardInf.temp[tempNo] < -400 ? 0 : isoBoardInf.temp[tempNo]);
    }

    isoBoardInf.volGridAB = pBuf[ofs];
    ofs += 1;
    isoBoardInf.volGridBC = pBuf[ofs];
    ofs += 1;
    isoBoardInf.volGridCA = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoResPos = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoResNeg = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curGridA = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curGridB = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curGridC = pBuf[ofs];
    ofs += 1;
    isoBoardInf.curLeak = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoVolPos = pBuf[ofs];
    ofs += 1;
    isoBoardInf.isoVolNeg = pBuf[ofs];

    SetIsoBoardInfor(&isoBoardInf);

    for(tempNo = 0; tempNo < 8; tempNo++) 
    {
		printf("\ttemp%d[%d]\n", tempNo, isoBoardInf.temp[tempNo]);
	}
	printf("\tvolGridAB[%d]\n", isoBoardInf.volGridAB);
	printf("\tvolGridBC[%d]\n", isoBoardInf.volGridBC);
	printf("\tvolGridCA[%d]\n", isoBoardInf.volGridCA);
	printf("\tisoResPos[%d]\n", isoBoardInf.isoResPos);
	printf("\tisoResNeg[%d]\n", isoBoardInf.isoResNeg);
	printf("\tcurGridA[%d]\n", isoBoardInf.curGridA);
	printf("\tcurGridB[%d]\n", isoBoardInf.curGridB);
	printf("\tcurGridC[%d]\n", isoBoardInf.curGridC);
	printf("\tcurLeak[%d]\n",  isoBoardInf.curLeak);
	printf("\tisoVolPos[%d]\n", isoBoardInf.isoVolPos);
	printf("\tisoVolNeg[%d]\n", isoBoardInf.isoVolNeg);
    return;
}

void ModbusRequest_ISOBoardAllInfor(modbus_t *ctx, uint16_t *pBuf)
{
    Modbus_info_t iso_info;
    int ret = 0; 
    const ConfigData *config_data = get_config_data_instance();
    
    iso_info.sid = (config_data->iso).slave_id;
    iso_info.addr = 0;
    iso_info.fc = 0x03;
    iso_info.num = 19;
    iso_info.val = 0;

    ret = modbus_send_message_handler(ctx, &iso_info, pBuf);
    if(0 == ret)
    {
        printf("[%s][%d] modbus send message handler fail!\n", __func__, __LINE__);
    }else
    {
        CommMonitor_AddHeartbeat(COMM_DEVICE_ISO_BOARD);
        printf("[%s][%d] successfully send [%d] bytes\n", __func__, __LINE__, ret);
        ParseModbusReply_ISOBoardAllInfor(pBuf);
    }
}
#endif

void master_comm_do(modbus_t *ctx, const mc_handle_t *pMcHandle)
{
    if(NULL == ctx || NULL == pMcHandle)
    {
        printf("[%s][%d] master comm do fail\n", __func__, __LINE__);
        return;
    }
    uint16_t *pBuf = sendRecvBuffer;

    if(pMcHandle->optType != OT_READ && pMcHandle->optType != OT_WRITE) 
    {
        printf("[%s][%d] mcHandleTable[%d] opt type defination error\n", __func__, __LINE__, pMcHandle->indexNo);
        return;
    }

    if(pMcHandle->do_modbus_send != NULL)
    {
        pMcHandle->do_modbus_send(ctx);
    }else
    {
        pMcHandle->do_modbus_recv(ctx, pBuf);
    }
}